#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>

#include "../../Profiler.h"

struct ProfilingListener : CppUnit::TestListener {
    void startTest(CppUnit::Test* test) {
        Profiler::clear();
        Profiler::start(test->getName() + " time");        
    }
    
    void endTest(CppUnit::Test* test) {
        Profiler::stop(test->getName() + " time"); 
        std::cout << Profiler::print() << std::endl;
    }
};

#include <conio.h>

int main(int argc, char *argv[]) {
	//printf("%s\n", argv[0]);

	// Create the event manager and test controller
	CPPUNIT_NS::TestResult controller;

	// Add a listener that collects test result
	CPPUNIT_NS::TestResultCollector result;
	controller.addListener(&result);

	// Add a listener that print dots as test run.
	CPPUNIT_NS::BriefTestProgressListener progress;
	controller.addListener(&progress);
	
	// Add profiling listener
    ProfilingListener listener;
    controller.addListener(&listener);

	// Add the top suite to the test runner
	CPPUNIT_NS::TestRunner runner;
		
	runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
	runner.run(controller);

	// Print test in a compiler compatible format.
	CPPUNIT_NS::CompilerOutputter outputter(&result, CPPUNIT_NS::stdCOut());
	outputter.write();

	//getch();
	return result.wasSuccessful() ? 0 : 1;
}
